<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会员ID" prop="membershipId">
        <el-input
            v-model="queryParams.membershipId"
            placeholder="请输入会员ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['membership:valuestorage:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['membership:valuestorage:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['membership:valuestorage:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['membership:valuestorage:export']"
        >导出
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            icon="Plus"-->
<!--            @click="handleChongzhi"-->
<!--            v-hasPermi="['membership:valuestorage:chongzhi']"-->
<!--        >充值-->
<!--        </el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="valuestorageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="储值ID" align="center" prop="storageId"/>
      <el-table-column label="会员卡号" align="center" prop="membership.membershipCardNumber"/>
      <el-table-column label="会员类型" align="center" prop="membership.membershipType"/>
      <el-table-column label="会员等级" align="center" prop="membershipLevel.levelName"/>
      <el-table-column label="会员姓名" align="center" prop="patient.realName"/>
      <el-table-column label="手机号" align="center" prop="patient.phoneNumber"/>
      <el-table-column label="累计储值" align="center" prop="valueStorageChangeRecord.cumulativeRecharge"/>
      <el-table-column label="储值余额" align="center" prop="storageBalance"/>
      <el-table-column label="累计消费" align="center" prop="membership.totalConsumption"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['membership:valuestorage:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['membership:valuestorage:remove']">删除
          </el-button>
          <el-button type="primary" size="small" @click="chongzhitanchuang(scope.row)">充值</el-button>
          <el-button type="danger" size="small" @click="tuikuantanchuang(scope.row)">退款</el-button>

        </template>

      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改储值管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="valuestorageRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="储值余额" prop="storageBalance">
          <el-input v-model="form.storageBalance" placeholder="请输入储值余额"/>
        </el-form-item>
        <el-form-item label="储值变动记录ID" prop="storageChangeRecordId">
          <el-input v-model="form.storageChangeRecordId" placeholder="请输入储值变动记录ID"/>
        </el-form-item>
        <el-form-item label="会员ID" prop="membershipId">
          <el-input v-model="form.membershipId" placeholder="请输入会员ID"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!--充值弹窗-->
    <el-dialog :title="title1" v-model="open1" width="500px" append-to-body>
      <el-form ref="membershipRef" :model="formcz" :rules="rules" label-width="80px">
        <el-form-item label="患者id" prop="patientId">
          <el-input v-model="formcz.patientId" placeholder="请输入患者id" />
        </el-form-item>
        <el-form-item label="增加金额" prop="zengjiajine">
          <el-input v-model="formcz.zengjiaJine" placeholder="请输入充值金额" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="abd">确 定</el-button>
          <el-button @click="quxiao">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!--退款弹窗-->
    <el-dialog :title="title2" v-model="open2" width="500px" append-to-body>
      <el-form ref="membershipRef" :model="formcz" :rules="rules" label-width="80px">
        <el-form-item label="患者id" prop="patientId">
          <el-input v-model="formcz.patientId" placeholder="请输入会员id" />
        </el-form-item>
        <el-form-item label="退款金额" prop="jianshaoJine">
          <el-input v-model="formcz.jianshaoJine" placeholder="请输入退款金额" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="jbd">确 定</el-button>
          <el-button @click="quxiao">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Valuestorage">
import {
  listValuestorage,
  getValuestorage,
  delValuestorage,
  addValuestorage,
  updateValuestorage, tuikuandialog,chongzhidialog

} from "@/api/membership/valuestorage";
import {updatePatient} from "@/api/sn/patient"
const {proxy} = getCurrentInstance();

const valuestorageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const title1 = ref("");
const title2 = ref("");
const open1 = ref(false);
const open2 = ref(false);

const data = reactive({
  form: {},
  formcz: {
    patientId:null,
    zengjiaJine:null,
    jianshaoJine:null
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    membershipId: null,
  },
  rules: {}
});

const {queryParams, form, rules,formcz} = toRefs(data);

/** 查询储值管理列表 */
function getList() {
  loading.value = true;
  listValuestorage(queryParams.value).then(response => {
    valuestorageList.value = response.rows;
    total.value = response.total;
    console.log(valuestorageList.value)
    loading.value = false;
  });
}

//充值前查询
// function handleChongzhi(){
//   loading.value = true;
//   chonghziqianchaxun(queryParams.value).then(response => {
//     console.log(response)
//     loading.value = false;
//     form.value = response.data;
//     open.value = true;
//     title.value = "充值";
//   });
// }


// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    storageId: null,
    storageBalance: null,
    storageChangeRecordId: null,
    membershipId: null,
    ext1: null,
    ext2: null,
    ext3: null,
    ext4: null,
    ext5: null
  };
  proxy.resetForm("valuestorageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.storageId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加储值管理";
}


/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _storageId = row.storageId || ids.value
  getValuestorage(_storageId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改储值管理";
  });
}

// 充值弹窗
function chongzhitanchuang(row){
  open1.value = true;

}
// 确认充钱
function abd(){
  chongzhidialog(formcz.value).then(response => {
    proxy.$modal.msgSuccess("充值成功");
    open1.value = false;
    getList();
  });
}

// 退款弹窗
function tuikuantanchuang(row){
  open2.value = true;

}
// 确认退款
function jbd(){
  tuikuandialog(formcz.value).then(response => {
    proxy.$modal.msgSuccess("退款成功");
    open2.value = false;
    getList();
  });
}
// 取消充钱
function quxiao(){
  open1.value = false;
  open2.value = false;
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["valuestorageRef"].validate(valid => {
    if (valid) {
      if (form.value.storageId != null) {
        updateValuestorage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addValuestorage(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _storageIds = row.storageId || ids.value;
  proxy.$modal.confirm('是否确认删除储值管理编号为"' + _storageIds + '"的数据项？').then(function () {
    return delValuestorage(_storageIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('membership/valuestorage/export', {
    ...queryParams.value
  }, `valuestorage_${new Date().getTime()}.xlsx`)
}

getList();
</script>
